/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package coalescent.cef.command.listener.exe;

import cef.command.Command;
import cef.command.listener.exe.CommandExeEvent;
import cef.command.listener.exe.CommandExeListener;
import cef.command.listener.exe.FigureEditorExeComponent;

import coalescent.cef.command.RunBackwardRecursionCommand;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;

import java.util.List;

/**
 * @author Susanta Tewari
 */
public class VisualizeRecursionCacheDistnFx extends FigureEditorExeComponent {

    /** Field description */
    private LineChart<Number, Number> lc;

    /**
     * Method description
     *
     *
     * @param cmd
     */
    @Override
    public void initComponents(Command cmd) {

        title = "Recursion Cache Distribution";

        final JFXPanel jfxPanel = new JFXPanel();

        figure = jfxPanel;

        Platform.runLater(new Runnable() {

            @Override
            public void run() {
                initFX(jfxPanel);
            }

        });
    }


    // <editor-fold defaultstate="collapsed" desc="Supporting initComponents()">

    /**
     * Method description
     *
     *
     * @param fxPanel
     */
    private void initFX(JFXPanel fxPanel) {


        // This method is invoked on JavaFX thread
        Group root  = new Group();
        Scene scene = new Scene(root);

        root.getChildren().add(createChart());
        fxPanel.setScene(scene);
    }

    /**
     * Method description
     *
     *
     * @return
     */
    private LineChart<Number, Number> createChart() {

        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();

        lc = new LineChart<>(xAxis, yAxis);

        lc.setAnimated(false);


        // setup chart
        lc.setTitle(title);
        xAxis.setLabel("Cut-Set");
        yAxis.setLabel("CacheSize");

        XYChart.Series<Number, Number> series = new XYChart.Series<>();

        lc.getData().add(series);

        return lc;
    }


    // </editor-fold>

    /**
     * Method description
     *
     *
     * @param event
     */
    @Override
    public void updateComponentData(CommandExeEvent event) {

        if (event.getSource() instanceof RunBackwardRecursionCommand) {

            RunBackwardRecursionCommand command = (RunBackwardRecursionCommand) event.getSource();
            final List<Integer> cacheSize       = command.getCurrentRecursionCacheSize();


            // ass data to the series
            Platform.runLater(new Runnable() {

                @Override
                public void run() {

                    XYChart.Series<Number, Number> series = new XYChart.Series<>();

                    for (int i = 0; i < cacheSize.size(); i++) {

                        series.getData().add(new XYChart.Data<Number, Number>(Integer.valueOf(i),
                                cacheSize.get(i)));
                    }

                    lc.getData().set(0, series);
                }

            });
        }
    }

    /**
     * Method description
     *
     *
     * @return
     */
    @Override
    public Class<? extends Command> getTargetCommandClass() {
        return RunBackwardRecursionCommand.class;
    }

    /**
     * Method description
     *
     *
     * @return
     */
    @Override
    public CommandExeListener newInstance() {
        return new VisualizeRecursionCacheDistnFx();
    }
}
